package com.china08.yxyapi.security;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.J2EContext;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.springframework.web.SecurityInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.SpringContextUtil;

/**
 * Jwt Profile Util
 * 
 * @author Ertao.Fang
 *
 */
public class JwtProfileUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(JwtProfileUtil.class);

    /**
     * 获取Profile:单个
     * 
     * @param request
     * @param response
     * @return
     */
    public static YxyProfile getProfile(HttpServletRequest request, HttpServletResponse response) {
        YxyProfile profile = new YxyProfile();

        // ##
        // 检测授权参数:是否为空
        String headerToke = StringUtils.trimToEmpty(request.getHeader(HttpConstants.AUTHORIZATION_HEADER));
        String paramToken = StringUtils.trimToEmpty(request.getParameter("token"));
        headerToke = StringUtils.trimToEmpty(headerToke.replaceAll("Bearer", ""));
        if (StringUtils.isBlank(headerToke) && StringUtils.isBlank(paramToken)) {//
            return profile;
        }

        List<YxyProfile> profiles = getProfiles(request, response);
        if (profiles.size() > 0) { //
            profile = profiles.get(0);
        } else {
            profile = getProfileWithCustomInterceptor(request, response);
        }
        return profile;
    }

    /**
     * 获取Profile:所有
     * 
     * @param request
     * @param response
     * @return
     */
    private static List<YxyProfile> getProfiles(HttpServletRequest request, HttpServletResponse response) {
        WebContext context = new J2EContext(request, response);
        final ProfileManager<YxyProfile> manager = new ProfileManager<YxyProfile>(context);
        List<YxyProfile> commonProfiles = manager.getAll(true);
        return commonProfiles;
    }

    /**
     * 
     * 获取Profile:单个<br>
     * 
     * 解决路径拦截规则中, 不需要登录就能访问的接口, 获取不到Profile的问题
     * 
     * @param request
     * @param response
     * @return
     */
    private static YxyProfile getProfileWithCustomInterceptor(HttpServletRequest request,
            HttpServletResponse response) {
        YxyProfile yxyProfile = new YxyProfile();
        Config config = SpringContextUtil.getBean(Config.class);
        try {
            new SecurityInterceptor(config, "HeaderClient").preHandle(request, response, null);
        } catch (Exception e) {
            // LOGGER.error("", e);
            LOGGER.error("解析Jwt异常:" + e.getMessage());
        }
        List<YxyProfile> commonProfiles = getProfiles(request, response);
        if (commonProfiles.size() > 0) {
            yxyProfile = commonProfiles.get(0);
        }
        return yxyProfile;
    }
}
